package com.sanmubird.mywebframework.eventbus.core;


import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class DefaultPublisher implements MqPublisher {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public <T> void publish(MqMessage<T> mqMessage) {
        String routingKey = mqMessage.getRoutingKey();
        MqExchangeEnum mqExchangeEnum = MqExchangeEnum.getRouterBy(routingKey);
        String queueName = mqExchangeEnum.getQueueName();
        //  延迟队列需要结合具体的实际业务场景 此处暂不实现 可以通过自定义Publisher实现
        if (mqExchangeEnum.getExchangeType().equals(MqExchangeTypeEnum.DIRECT)) {
            stringRedisTemplate.opsForList().leftPush(queueName, JSON.toJSONString(mqMessage));
        }
    }
}
